/* project:			package:		class:			credits:					date:
 * GuildMaster		skin			ButtonSkin		by Dolraith & Kirenno		Aug 2011
 * -- contains information needed to skin a button and related methods
 * 
 * === PUBLIC FNs ===================================================================
 * 	public ButtonSkin ()	-- constructor
 * 	public String getImgPath (String state, int part)	-- returns image path for button state and part
 * 														valid state values: normal, rolled, hover, pressed, down, pressedRolled, downHover, disabled, dead
 * 	public String getImgPath (int state, int part)	-- returns image path for button state and part
 * 														valid state values: 0=normal, 1=rolled, 2=pressed, 3=pressedRolled, 4=disabled
 * 	public void parseInput (LinkedList <String> openTags, String curTag, String content) -- loads XML values into the skin
 *  
 */



package skin;

import java.awt.Color;
import java.util.LinkedList;

import tools.StringTools;

public class ButtonSkin {
	// === GLOBAL CLASS VARIABLES =================================================================
	private int buttonImagesPerState;
	private ButtonStateSkin stateNormal;
	private ButtonStateSkin stateRolled;
	private ButtonStateSkin statePressed;
	private ButtonStateSkin statePressedRolled;
	private ButtonStateSkin stateDisabled;
	
	// === CONSTRUCTOR, INITIALIZERS & resets =====================================================
	public ButtonSkin () {							// constructor
		setButtonImagesPerState (1);						// initialize global variables
		stateNormal = new ButtonStateSkin("normalState");
		stateRolled = new ButtonStateSkin("rolledState");
		statePressed = new ButtonStateSkin("pressedState");
		statePressedRolled = new ButtonStateSkin("pressedRolledState");
		stateDisabled = new ButtonStateSkin("disabledState");
	}
	
	// === GETTERS ================================================================================
	public int getButtonImagesPerState () {						// returns the value of buttonImagesPerState
		return buttonImagesPerState; 	
	}
	public String getImgPath (String state, int part) {			// returns the path to the image representing the specified part in the specified state
		// TODO make this string switch
		if (state.equalsIgnoreCase("normal")) 
			return getImgPath (0, part);
		else if (state.equalsIgnoreCase("rolled") || state.equalsIgnoreCase("hover")) 
			return getImgPath (1, part);
		else if (state.equalsIgnoreCase("pressed") || state.equalsIgnoreCase("down")) 
			return getImgPath (2, part);
		else if (state.equalsIgnoreCase("pressedRolled") || state.equalsIgnoreCase("downHover")) 
			return getImgPath (3, part);
		else if (state.equalsIgnoreCase("disabled") || state.equalsIgnoreCase("dead")) 
			return getImgPath (4, part);
		else return getImgPath (-1, part);
	}
	public String getImgPath (int state, int part) {			// returns the path to the image representing the specified part in the specified state 
		String result = "";
		switch (state) {
			case 0: result = stateNormal.getImgPartPath(part); break;
			case 1: result = stateRolled.getImgPartPath(part); break;
			case 2: result = statePressed.getImgPartPath(part); break;
			case 3: result = statePressedRolled.getImgPartPath(part); break;
			case 4: result = stateDisabled.getImgPartPath(part); break;
			default: result = "";		break;
		}
		if (result.isEmpty())
			Skin.reportSkinError("skin.button (getImgPath)", "encountered invalid button state while reading skin object (state = "+state+")");
		
		return result;
	}
	public Color getFontColor (String state) {			// returns the font color in the specified state
		// TODO make this string switch
		if (state.equalsIgnoreCase("normal")) 
			return getFontColor (0);
		else if (state.equalsIgnoreCase("rolled") || state.equalsIgnoreCase("hover")) 
			return getFontColor (1);
		else if (state.equalsIgnoreCase("pressed") || state.equalsIgnoreCase("down")) 
			return getFontColor (2);
		else if (state.equalsIgnoreCase("pressedRolled") || state.equalsIgnoreCase("downHover")) 
			return getFontColor (3);
		else if (state.equalsIgnoreCase("disabled") || state.equalsIgnoreCase("dead")) 
			return getFontColor (4);
		else return getFontColor (-1);
	}
	public Color getFontColor (int state) {			// returns the the font color in the specified state 
		Color result = Color.GRAY;
		switch (state) {
			case 0: result = stateNormal.getFontColor(); break;
			case 1: result = stateRolled.getFontColor(); break;
			case 2: result = statePressed.getFontColor(); break;
			case 3: result = statePressedRolled.getFontColor(); break;
			case 4: result = stateDisabled.getFontColor(); break;
			default: {
				Skin.reportSkinError("skin.button (getFontColor)", "encountered invalid button state while reading skin object (state = "+ state + ")");
				break;
			}
		}
		if (result == null) {
			result = Color.GRAY;
			Skin.reportSkinError("skin.button (getFontColor)", "font color is null (state = " + state + "); resetting font color to gray");
		}
		return result;
	}
	
	// === SETTERS ================================================================================
	public void setButtonImagesPerState (int value) {
		buttonImagesPerState = value;
	}
	
	// === IO METHODS & parsers ===================================================================
	
	public void parseInput (LinkedList <String> openTags, String curTag, String content) {	// XML input parser
		if (content.isEmpty()) return;							// skip empty entries		
		@SuppressWarnings("unchecked")							// copy the tag structure (so removing things wont hurt the original)
		LinkedList <String> cloneOpenTags = (LinkedList<String>) openTags.clone();
		while (!cloneOpenTags.isEmpty()) {						// go through the list from outter edge to inner,
																//			sending pieces to where they belong
			if (cloneOpenTags.getFirst().equals("button") || cloneOpenTags.getFirst().equals("buttons")) 
				cloneOpenTags.removeFirst();						// remove "button" tag
			if (cloneOpenTags.isEmpty()) {							// if no further tags are present, send to this module
				processXMLTag (cloneOpenTags, curTag, content);				
				return;
			}			
			
			// handle known tags			
			if (cloneOpenTags.getFirst().equals("normalState")) {
				stateNormal.parseInput(cloneOpenTags, curTag, content);
				return;
			} else if (cloneOpenTags.getFirst().equals("rolledState")) {
				stateRolled.parseInput(cloneOpenTags, curTag, content);
				return;
			} else if (cloneOpenTags.getFirst().equals("pressedState")) {
				statePressed.parseInput(cloneOpenTags, curTag, content);
				return;
			} else if (cloneOpenTags.getFirst().equals("pressedRolledState")) {
				statePressedRolled.parseInput(cloneOpenTags, curTag, content);
				return;
			} else if (cloneOpenTags.getFirst().equals("disabledState")) {
				stateDisabled.parseInput(cloneOpenTags, curTag, content);
				return;
			} else {					// skip unknown tags
				Skin.reportSkinError("skin.button object", "XML tag not recognized (tag = ".concat(cloneOpenTags.getFirst()).concat("); dropping tag"));
				cloneOpenTags.removeFirst();
			}
		}
																// when the tag structure isn't workable, give error
		String error = "->";
		for (int i=0; i<openTags.size(); i++)
			error = error.concat("->").concat(openTags.get(i));
		error = error.substring(2);
		Skin.reportSkinError("skin.button object", "XML structure not recognized (".concat(error).concat(")"));
	}
	private void processXMLTag (LinkedList <String> openTags, String curTag, String content) {// process XML tag that belong in this class
		if (openTags.isEmpty() && curTag.equals("imagesPerState")) {
			int value = StringTools.convertStrToInt(content);
			if (value < 0) {
				Skin.reportSkinError("skin.ButtonSkin object", "invalid tag value for imagesPerState, its supposed to be an integer (value = ".concat(content).concat("); imagesPerState value reset to 1"));
				value = 1;
			}
			setButtonImagesPerState (value);
			return;
		}
					// if tag is not recognized 
		System.out.println("ERROR in ButtonSkin.processXMLTag: There is no handler for this XML tag (tag = ".concat(curTag).concat(")"));
	}

//	public void parseInput (String[] parsedCfg) {				// non-XML input parser
		// parsedCfg: 0 will always have the config name, everything after are vals	
//		if (parsedCfg[0].equalsIgnoreCase("standardButtonImage")) {
//			stateNormal.parseInput(parsedCfg);
//		} else if(parsedCfg[0].equalsIgnoreCase("standardButtonImage_rolled")) {
//			stateRolled.parseInput(parsedCfg);
//		} else if(parsedCfg[0].equalsIgnoreCase("standardButtonImage_pressed")) {
//			statePressed.parseInput(parsedCfg);
//		} else if(parsedCfg[0].equalsIgnoreCase("standardButtonImage_pressedRolled")) {
//			statePressedRolled.parseInput(parsedCfg);
//		} else if(parsedCfg[0].equalsIgnoreCase("standardButtonImage_disabled")) {
//			stateDisabled.parseInput(parsedCfg);
//		} else Skin.reportSkinError (parsedCfg[0], "element not recognized");		
//	}
	
}
